#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QPropertyAnimation>
#include<QFile>
#include<QDebug>
LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));
    animation=new QPropertyAnimation(this,"geometry",this);
    animation->setDuration(250);
    animation->setStartValue(QRect(10,10,width(),height()));
    animation->setEndValue(QRect(10,10+height(),width(),height()));
    connect(ui->hideBtn,&QPushButton::clicked,this,[=](){
        animation->start();
    });
    connect(animation,&QPropertyAnimation::finished,this,[=](){
       hide();
    });
    showLrcWord(-1);
}

bool LrcPage::parseLrc(const QString& Path)
{
    QFile file(Path);
    if(!file.open(QIODevice::ReadOnly))
    {
        return false;
    }
    musicLrcwords.clear();
    while(!file.atEnd())
    {
        int start=0;
        int end=0;
        qint64 mmtime=0;
        QString line=file.readLine(1024);
        //1.通过查找“]”来分割时间与歌词文本
        //2.分割时间中的时分秒
        //3.将该行保存
        int left =line.indexOf('[');
        int right=line.indexOf(']');
        QString time=line.mid(left,right-left+1);
        //解析分钟
        start= 1;
        end=time.indexOf(':',start);
        mmtime+=time.mid(start,end-start).toInt()*60*1000;
        //解析秒
       start=end+1;
       end = time.indexOf('.',start);
       mmtime+=time.mid(start,end-start).toInt()*1000;
       //解析毫秒
       start=end+1;
       end = time.indexOf('.',start);
       mmtime+=time.mid(start,end-start).toInt();
       QString word=line.mid(right+1,line.size()-right-1-1).trimmed();
       musicLrcwords.push_back(LrcWorldLine(mmtime,word));
    }
    for(auto e : musicLrcwords)
       {
           qDebug()<<e.time<<":"<<e.text;
       }
    return true;
}

int LrcPage::getLineWordIndex(qint64 time)
{
    if(musicLrcwords.empty())
    {
        return -1;
    }
    if(musicLrcwords[0].time>=time)
    {
        return 0;
    }
    for(int i=1;i<musicLrcwords.size();i++)
    {
        if(time>=musicLrcwords[i-1].time&&time<musicLrcwords[i].time)
        {
            return i-1;
        }
    }
    return musicLrcwords.size()-1;
}

QString LrcPage::getlineWordbyIndex(qint64 index)
{
    if(index < 0 || index >= musicLrcwords.size())
    {
       return "";
    }
    return musicLrcwords[index].text;
}

void LrcPage::showLrcWord(int time)
{
    int index=getLineWordIndex(time);
    if(-1 == index)
     {
     ui->line1->setText("");
     ui->line2->setText("");
    ui->line3->setText("");
     ui->lineCenter->setText("当前歌曲⽆歌词");
     ui->line4->setText("");
    ui->line5->setText("");
     ui->line6->setText("");
    }else
    {
     ui->line1->setText(getlineWordbyIndex(index-3));
     ui->line2->setText(getlineWordbyIndex(index-2));
     ui->line3->setText(getlineWordbyIndex(index-1));
     ui->lineCenter->setText(getlineWordbyIndex(index));
    ui->line4->setText(getlineWordbyIndex(index+1));
    ui->line5->setText(getlineWordbyIndex(index+2));
     ui->line6->setText(getlineWordbyIndex(index+3));
    }

}

LrcPage::~LrcPage()
{
    delete ui;
}
